/***
NOTES

Input files: 
> comp_covenants (generated in 0_2_Sample_Creation_Quarterly.sas)
> COMP_Q_dttd_violations (generated in `0_2_Sample_Creation_Quarterly.do')

Output files: 
> __covenant_slack_SRDD.csv // For the spatial RDD 
> __covenant_slack2.csv // For the quarterly analysis


***/

*** -----------------------------------------------------------------------***;
*** -----------------------------------------------------------------------***;
***  STATA CODE PART STARTS HERE 													   ***;
***  --------- QUARTERLY DATA ---------											   ***;
*** -----------------------------------------------------------------------***;
*** -----------------------------------------------------------------------***;
global dir your_path
cd "$dir"


use "comp_covenants", clear


	merge 1:1 gvkey datadate using "COMP_Q_dttd_violations.dta", keepusing(dttd dttd2)
	drop if _merge==2 
	drop _merge 


gen fiscal_date=yq(fyearq, fqtr)
format fiscal_date %tq

duplicates tag gvkey fiscal_date, gen(dup)
drop if dup>0

sort gvkey fiscal_date
xtset gvkey fiscal_date


gen slack_tangnetworth_pct=slack_tangnetworth/abs(tangible_net_worth_d)
gen slack_cur_ratio_pct=slack_cur_ratio/abs(min_cur_ratio)
gen slack_ebitda_debt_pct=slack_ebitda_debt/abs(min_ebitda_debt)
gen slack_pct=min(slack_tangnetworth_pct,slack_cur_ratio_pct)

* set missing slack as an arbitrarily large fraction:
replace slack_pct=99 if slack_pct==. 
gen violation=(slack_pct<0)
destring sic, replace 


winsor2 capxq capxq4 atq atq_1 saleq, replace 
gen inv=capxq4/atq_1
gen ln_assets=ln(atq)
gen ln_assets_lag1y=ln(atq_1)
gen ln_sale=ln(saleq)
gen inv_lead=f.inv
gen slack_net_worth_pct = slack_net_worth/abs(net_worth_d)
gen slack_pct2=min(slack_tangnetworth_pct,slack_net_worth_pct, slack_cur_ratio_pct)
replace slack_pct2=99 if slack_pct2==. 
gen slack_pct2_lag=l4.slack_pct2

*** HYBRID violation variable  
	gen new_slack_pct2=slack_pct2
	replace new_slack_pct2=abs(rnormal(0,0.1))  if viol_confirmed==0 & violation_short==1


**** --------- For the spatial RDD --------- ****
preserve

		keep gvkey datadate sic fiscal_date inv_lead inv violation ln_sale  ln_assets_lag1y  capxq4 slack_tangnetworth_pct slack_ebitda_debt_pct ///
		     slack_cur_ratio_pct slack_pct  ebitda_debt networth2 tangnetworth debt_tangnetworth cur_ratio debt_equity  ///
		     quick_ratio xrdq4 fyearq fqtr saleq equity totaldebt capxq mb bl roa tan altmanz /// 
		     slack_pct2  new_slack_pct2 viol_confirmed violation_short
		
		winsor2 inv_lead inv ln_sale  ln_assets_lag1y  capxq4 tangnetworth debt_tangnetworth cur_ratio debt_equity  ///
		     quick_ratio xrdq4  saleq equity totaldebt capxq mb bl roa tan altmanz, replace
		
		winsor2 slack_pct2 if slack_pct2!=99, replace 
		winsor2 new_slack_pct2 if new_slack_pct2!=99, replace 
		
		foreach x in inv_lead ln_sale mb bl roa {
			drop if `x'==.
		}
		
		gen year=year(datadate)
		*** DROPPING and Filtering :
		drop if year<=1993
		drop if (sic >=6000 & sic < 7000) | (sic >= 3999 & sic < 5000)
		count
	
		export delimited using "__covenant_slack_SRDD.csv", replace

restore 
***/

*** KEEP ONLY YEARS>=1994
	gen year=year(datadate)
	keep if year>=1994

*** Define the panel: 
	gen yearq=yq(fyearq, fqtr)
	format yearq %tq
	destring(gvkey), replace	
	duplicates drop gvkey yearq, force // six duplicate obs.

	sort gvkey yearq
	xtset gvkey yearq


*** GENERATE + FIX VARIABLES 
	by gvkey year, sort: egen viol_confirmed_y=max(viol_confirmed)
	sort gvkey yearq
	replace xrdy=0 if xrdy==. 
	replace xrdq=0 if xrdq==. 
	replace xrdq4=0 if xrdq4==. 
	destring(sic), replace 
	
	winsor2 atq saleq ch tan roa bl mb altmanz capxy  capxq  capxq4  ppentq xrdq xrdq xrdq4, replace
	gen ln_atq=ln(atq)
	gen ln_sales=ln(saleq)
	* Fix some issues with networth: 
	gen slack_networth2=slack_net_worth
	replace slack_networth2=slack_tangnetworth if slack_net_worth==.


*** DROP WHAT? 
	drop if (sic >=6000 & sic < 7000) | (sic >= 3999 & sic < 5000)

*** Definitions of Investment used here: 
	gen capxq4_at=(capxq4)/l.atq
	winsor2 capxq4_at, replace 

*** Standardize variables: 
	foreach x in slack_int_cov slack_cur_ratio slack_ebitda_debt slack_debt_tangnetworth slack_fx_ch_cov slack_quick_ratio slack_debt_equity slack_capex slack_ebitda slack_tangnetworth slack_net_worth slack_networth slack_networth2  {
		replace `x'=. if `x'==999
		winsor2 `x', replace
		replace `x'=999 if `x'==.
		gen `x'_lag=L.`x'
	}
*** Generate % distances from the threshold:
	winsor2 tangible_net_worth_d min_cur_ratio net_worth_d min_networth capxq4 capxy, replace 
	
	replace slack_cur_ratio_pct   	=slack_cur_ratio/min_cur_ratio
	replace slack_tangnetworth_pct  =slack_tangnetworth/tangible_net_worth_d

	gen slack_networth_pct		=slack_networth/min_networth
	gen slack_networth2_pct		=slack_networth2/min_networth
	gen slack_capex_pct			=slack_capex/capxq4
	gen slack_capex2_pct		   =slack_capex/capxy
	winsor2 slack_cur_ratio_pct slack_networth_pct slack_networth2_pct slack_capex_pct slack_capex2_pct, replace

	foreach x in slack_cur_ratio_pct slack_net_worth_pct slack_tangnetworth_pct {
		gen `x'_abs=abs(`x')
	}


	foreach x in slack_cur_ratio_pct slack_net_worth_pct slack_tangnetworth_pct slack_networth_pct slack_networth2_pct slack_capex_pct slack_capex2_pct {
		replace `x'=999 if `x'==.
		gen `x'_lag=L.`x'

	}


*** Covenant Controls + Polynomials:
	gen ocf_at 		 = oibdpq/l.atq
	gen debt_at 	 = (dlcq+dlttq)/l.atq
	gen networth_at  = (atq-ltq)/atq
	gen intexp_at    = xintq/atq
	gen mtb 		 = ((prccq*cshoq)+(ltq))/atq
	gen currentratio = actq/lctq 

	* Polynomials:
	foreach x in ocf_at debt_at networth_at intexp_at  mtb currentratio  {
		winsor2 `x', replace
		gen `x'_lag=L.`x'
	}

	gen viol_confirmed_lag=L4.viol_confirmed
	gen ln_sales_lag=l.ln_sales
	gen ch_lag=l.ch 
	gen capx_at=capxq/l.atq 
	gen altmanz_lag=l.altmanz
	gen has_capex_cov=(slack_capex!=999 & slack_capex!=.)
	gen l_has_capex_cov=l.has_capex_cov
	gen bind_slack_pct2s=(slack_pct2<0) if slack_pct2!=. & slack_pct2!=99 
	gen bind_new_slack_pct2s=(new_slack_pct2<0) if new_slack_pct2!=. & new_slack_pct2!=99 

	foreach x in atq saleq ch altmanz roa mtb bl {
		gen l_`x'=l.`x'
		winsor2 `x' l_`x', replace
	}

	gen ln_saleq=ln(1+saleq)
	gen l_logSale=l.ln_saleq
	gen l_cash=l.ch
	gen l_zscore=l.altmanz
	gen l_mb=l.mb
	gen l_dttd=l.dttd
	gen l_dttd2=l.dttd2


	keep if fyear>=1994
	sort yearq
	egen time_id=group(yearq)
count


export delimited using "__covenant_slack2.csv", replace

*** Final Save
 compress
 save "__covenant_slack2_allVars.dta", replace 
